//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类;

public class demo_2874 {
    public demo_2874() {
    }

    public static void main(String[] args) {
        int[] nums = new int[]{10, 13, 6, 2};
        System.out.println(maximumTripletValue(nums));
    }

    public static long maximumTripletValue(int[] nums) {
        int n = nums.length;
        int[] iList = new int[n];
        int[] kList = new int[n];
        long max = -2147483648L;
        kList[n - 1] = nums[n - 1];

        for(int i = n - 2; i > 0; --i) {
            kList[i] = Math.max(nums[i], kList[i + 1]);
        }

        iList[0] = nums[0];

        for(int i = 1; i < n - 1; ++i) {
            iList[i] = Math.max(iList[i - 1], nums[i]);
            max = Math.max(max, (long)(iList[i - 1] - nums[i]) * (long)kList[i + 1]);
        }

        return max <= 0L ? 0L : max;
    }

    public static long maximumTripletValue2(int[] nums) {
        int n = nums.length;
        int[] kList = new int[n + 1];
        long max = -2147483648L;

        for(int i = n - 1; i > 0; --i) {
            kList[i] = Math.max(nums[i], kList[i + 1]);
        }

        int maxI = nums[0];

        for(int i = 1; i < n - 1; ++i) {
            max = Math.max(max, (long)(maxI - nums[i]) * (long)kList[i + 1]);
            maxI = Math.max(maxI, nums[i]);
        }

        return max <= 0L ? 0L : max;
    }
}
